<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Session;
use think\Db;
class Files extends Action
{
    public function index()
    {        
        
        $title = input('title');
        $cid = input('cid');
        $sid = input('sid');
        //读取分类信息
        $cateRes = model("type")->where("cid","=",1)->select();
        $cateRes2 = [];
        if($cid) $cateRes2 = model("type")->where("cid","=",$cid)->select();
        return $this->fetch('index',['title'=>$title,'cateRes'=>$cateRes,'cateRes2'=>$cateRes2,'cid'=>$cid,'sid'=>$sid]);

    }
    
    public function lists(){
        $map = [];
        $title = input('title');
        $cid = input('cid'); 
        $sid = input('sid');       
        if($title) $map["a.name"] = array("like","%$title%");
        if($cid)  $map["a.cid"] = array("=",$cid);
        if($sid)  $map["a.sid"] = array("=",$sid);
        $pagesize = input('pagesize'); 
        $pages = input('pages');
        if($pages<=1)
        $a = 0;
        else
        $a = $pagesize*($pages-1);
        $total = db("files")->alias("a")
                ->join("type b","a.cid=b.id","left")
                ->join("type c","a.sid=c.id","left")
                ->where($map)->count();
        $totalpage = ceil($total/$pagesize);
        $data = db("files")->alias("a")
                ->join("type b","a.cid=b.id","left")
                ->join("type c","a.sid=c.id","left")
                ->field('a.*,FROM_UNIXTIME(a.addtime,"%Y年%m月%d日") as addtime,b.name as cname,c.name as sname')
                ->where($map)
                ->order("a.vieworder desc,a.addtime desc")
                ->limit($a,$pagesize)
                ->select();
        //输出Json数据
        return json(['code'=>1,'message'=>"获取数据成功",'data'=>$data,'totalpage'=>$totalpage]);           

    }
    public function add(){
        if(request()->isPost()){
            $data["name"] = input("name"); 
            $data["addname"] = input("addname"); 
            $data["addtime"] = time(); 
            $data["vieworder"] = input("vieworder"); 
            $data["cid"] = input("cid"); 
            $data["sid"] = input("sid"); 
            $data["filesize"] = input("filesize"); 
            $data['filepath'] = model("Upload")->moveImg(input("filepath"));
            $save = db('files')->insert($data);
            if($save){            
                return backUrl();
            }
        }
        //读取分类信息
        $cateRes = model("type")->where("cid","=",1)->select();
        return $this->fetch('add',['cateRes'=>$cateRes]);

        
    }   

    public function edit(){
            $id = input("id");             
            if(request()->isPost()){
                $data["name"] = input("name"); 
                $data["addname"] = input("addname"); 
                $data["addtime"] = time(); 
                $data["vieworder"] = input("vieworder"); 
                $data["cid"] = input("cid"); 
                $data["sid"] = input("sid"); 
                $data["filesize"] = input("filesize"); 
                $data['filepath'] = model("Upload")->moveImg(input("filepath"));
                $save = db('files')->where('id','=',$id)->update($data);
                if($save!==false){
                    return backUrl();   
                }                
            }
            $data = db("files")->where('id',"=",$id)->field("*")->find();
            //读取分类信息
            $cateRes = model("type")->where("cid","=",1)->select();
            if($data['cid']) $cateRes2 = model("type")->where("cid","=",$data['cid'])->select();
            return $this->fetch('edit',['data'=>$data,'cateRes'=>$cateRes,'cateRes2'=>$cateRes2]);
    } 

    public function delete(){
        $id=input('id');
        $delete = db("files")->where("id","in",$id)->delete();
        if($delete)
            return 1;
        else
            return 0;

            
    }

     public function vieworderFunc(){

        $idArr=explode(",",input('id'));

        $vieworderArr=explode(",",input('vieworder'));

        $house_db = db("files");

        foreach ($idArr as $k => $v){

            $house_db->where("id","=",$v)->update(["vieworder"=>$vieworderArr[$k]]);

        }

        return 1;

    }

    public function Atrr(){

        $id=input('id');
        $data[input('field')] = input('value');
        $update = db("files")->where('id','in',$id)->update($data);
        if($update!==false){
            return 1;
        }else{
            return 0;
        }      

            
    }  
}
